package com.czd.support.job;

import com.czd.support.common.Configuration;
import com.czd.support.service.RoomService;
import com.czd.support.util.HttpClientUtils;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014-10-12 10:17)
 * Description:定期维护房间在线人数.
 */
@Component
public class SyncRoomUserCount {

    @Autowired
    private RoomService roomService;
    private static final Log log = LogFactory.getLog(SyncRoomUserCount.class);

    /**
     * 定期维护房间在线人数
     */
    @Scheduled(cron="0 0/10 7-23 * * ?")
    public void doAddAccountingDeptJob(){
        log.info("开始获取房间人数统计........."+Configuration.getInstance().getSync_room_user_count_url());
        String roomInfo = HttpClientUtils.doGet(Configuration.getInstance().getSync_room_user_count_url(),null);
        if (StringUtils.isBlank(roomInfo)) {
            log.info("暂无房间人数需要统计.......roomInfo:"+roomInfo);
            return;
        }
        JSONArray jsonArray = JSONArray.fromObject(roomInfo);
        log.error("更新开始===========================jsonArray:"+jsonArray);
        if(roomService.syncRoomUserCount(jsonArray)){
            log.error("更新成功===========================");
        }else{
            log.error("更新失败===========================");
        }
    }
}
